package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.fragments.BackServiceFragment;
import com.sxm.yiwei.fragments.NoServiceFragment;
import com.sxm.yiwei.fragments.SellSerivceFragment;

public class FWServiceInstrallActivity extends BaseActivity implements View.OnClickListener{

    private ImageView serviceInstrallback;
    private TextView serviceback;
    private TextView serviceno;
    private TextView servicesell;
    private SellSerivceFragment sellSerivceFragment;
    private NoServiceFragment noServiceFragment;
    private BackServiceFragment backServiceFragment;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private String shopid;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fwservice_instrall);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        shopid = sharedPreferences.getString("shopid", "");
        initServiceInstrallView();
        OnViewFragment(1);
        servicesell.setSelected(true);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
    }

    private void initServiceInstrallView() {
        backmain = (ImageView) findViewById(R.id.serviceinstrall_backmain);
        serviceInstrallback = (ImageView) findViewById(R.id.service_instrall_back);
        serviceback = (TextView) findViewById(R.id.service_back);
        serviceno = (TextView) findViewById(R.id.service_no);
        servicesell = (TextView) findViewById(R.id.service_sell);
        serviceback.setOnClickListener(this);
        serviceInstrallback.setOnClickListener(this);
        serviceno.setOnClickListener(this);
        servicesell.setOnClickListener(this);
        backmain.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.serviceinstrall_backmain:

                startActivity(new Intent(FWServiceInstrallActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.service_instrall_back:
                finish();
                break;
            case R.id.service_sell:
                servicesell.setSelected(true);
                serviceback.setSelected(false);
                serviceno.setSelected(false);
                OnViewFragment(1);
                break;
            case R.id.service_back:
                serviceback.setSelected(true);
                serviceno.setSelected(false);
                servicesell.setSelected(false);
                OnViewFragment(3);
                break;
            case R.id.service_no:
                serviceno.setSelected(true);
                servicesell.setSelected(false);
                serviceback.setSelected(false);
                OnViewFragment(2);
                break;
        }
    }

    private void OnViewFragment(int i) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        hideFragment(ft);
        switch (i){
            case 1:
                if (sellSerivceFragment==null){
                    sellSerivceFragment = new SellSerivceFragment();
                   ft.add(R.id.service_fragment,sellSerivceFragment);
                }else {
                    ft.show(sellSerivceFragment);
                }
                break;
            case 2:
                if (noServiceFragment==null){
                    noServiceFragment = new NoServiceFragment();
                    ft.add(R.id.service_fragment,noServiceFragment);
                }else {
                    ft.show(noServiceFragment);
                }
                break;
            case 3:
                if (backServiceFragment==null){
                    backServiceFragment = new BackServiceFragment();
                    ft.add(R.id.service_fragment,backServiceFragment);
                }else {
                    ft.show(backServiceFragment);
                }
                break;
        }
        ft.commit();

    }

    private void hideFragment(FragmentTransaction ft) {
        if (sellSerivceFragment!=null){
            ft.hide(sellSerivceFragment);
        }
        if (noServiceFragment!=null){
            ft.hide(noServiceFragment);
        }
        if (backServiceFragment!=null){
            ft.hide(backServiceFragment);
        }
    }

}
